<?php

declare(strict_types=1);

namespace app\admin\service\shop;

use app\common\model\goods\GoodsClassifyModel;
use app\common\model\goods\GoodsProductModel;
use app\common\model\system\PluginModel;
use app\common\model\user\UserCartModel;
use Dengje\Pxy\BaseService;

class GoodsService extends BaseService
{
    protected $model = null;


    public function __construct()
    {
        $this->model =  new \app\common\model\goods\GoodsModel();
    }

    public function list()
    {
        $where = [];
        $status = request()->get('status', 0);
        $id = request()->get('id');
        $name = request()->get('name', '');
        $intro = request()->get('intro', '');
        if ($status) $where[] = ['status', '=', $status];
        if ($id) $where[] = ['id', '=', $id];
        if ($name) $where[] = ['name', 'like', '%' . $name . '%'];
        if ($intro) $where[] = ['intro', 'like', '%' . $intro . '%'];
        $list = $this->model->where($where)->order(['weigh' => 'desc', 'id' => 'desc'])->paginate(request()->get('page_size/d', 10));
        $list->map(function ($v) {
            $classifyIds = $v['classify_ids'] ? explode(',', $v['classify_ids']) : [];
            $v['classifys'] = GoodsClassifyModel::whereIn('id', $classifyIds)
                ->column('name');
            return $v;
        });
        return $list;
    }

    public function detail($id)
    {
        $detail = $this->model->where('id', $id)->find();
        $detail['classify_ids'] = $detail['classify_ids'] ? explode(',', $detail['classify_ids']) : [];
        $detail['product'] = GoodsProductModel::where('goods_id', $detail['id'])->select();
        return $detail;
    }

    public function create($data)
    {
        $product = $data['product'];
        unset($data['product']);
        $data['create_time'] = time();
        if (@$data['id']) unset($data['id']);
        if (@$data['update_time']) unset($data['update_time']);
        $data['imgs'] = $this->model->setImgsAttr($data['imgs']);
        $data['tags'] = $this->model->setImgsAttr($data['tags']);
        $id = $this->model->insertGetId($data);
        self::goods_product($product, $id, $data['status'], 1);
        return $id;
    }

    public function update($id, $data)
    {
        $data = $this->preExcludeFields($data);
        if ($data['classify_ids']) {
            $data['classify_ids'] = is_array($data['classify_ids']) ? implode(',', $data['classify_ids']) : '';
        }
        GoodsProductModel::where('goods_id', $data['id'])->delete();
        self::goods_product($data['product'], $data['id'], $data['status']);
        unset($data['product']);
        if ($data['status'] == 0) {
            UserCartModel::where('goods_id', $data['id'])->update(['status' => 3]);
        }
        if (@$data['create_time']) unset($data['create_time']);
        $data['update_time'] = time();
        $data['imgs'] = $this->model->setImgsAttr($data['imgs']);
        $data['tags'] = $this->model->setImgsAttr($data['tags']);
        return $this->model->where('id', $id)->update($data);
    }

    public function delete($id)
    {
        $this->model->where('id', $id)->delete();
        GoodsProductModel::where('goods_id', $id)->delete();
        UserCartModel::where('goods_id', $id)->delete();
        return success('删除成功！');
    }

    public static function goods_product($product, $goodsId = 0, $goodsStatus = 0, $type = 0)
    {
        if (!empty($product) && $goodsId) {
            $productModel = [];
            foreach ($product as $k => $v) {
                if ($type == 0) $productModel[$k]['id'] = $v['id'] ? $v['id'] : '';
                $productModel[$k]['price'] = $v['price'];
                $productModel[$k]['line_price'] = $v['line_price'];
                $productModel[$k]['img'] = $v['img'];
                $productModel[$k]['name'] = $v['name'];
                $productModel[$k]['stock'] = $v['stock'];
                $productModel[$k]['weigh'] = $v['weigh'];
                $productModel[$k]['status'] = $goodsStatus == 1 ? $v['status'] : 2;
                $productModel[$k]['goods_id'] = $goodsId;
            }
            if (!empty($productModel)) (new GoodsProductModel())->insertAll($productModel);
        }
    }
}
